package com.css.fxfzypg.modules.rating.task.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.rating.library.model.vo.ExpertScoreProgressVO;
import com.css.fxfzypg.modules.rating.task.model.dto.*;
import com.css.fxfzypg.modules.rating.task.model.entity.KpRatingTask;
import com.css.fxfzypg.modules.rating.task.model.vo.*;
import com.css.fxfzypg.modules.rating.task.service.KpRatingTaskService;
import com.css.fxfzypg.web.PlatformSessionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 考评任务管理接口
 */
@RequestMapping("kpRatingTask")
@Slf4j
@RestController
public class KpRatingTaskController {

    @Autowired
    private KpRatingTaskService kpRatingTaskService;

    /**
     * 查询考评任务列表接口
     *
     * @return
     */
    @GetMapping("list")
    public RestResponse<List<KpRatingTask>> list(KpRatingTask kpRatingTask) {
        try {
            log.debug("查询考评任务列表，参数:{}", JSONObject.toJSONString(kpRatingTask));
            return RestResponse.succeed(kpRatingTaskService.list(kpRatingTask));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 考评任务详情
     *
     * @return
     */
    @GetMapping("getById")
    public RestResponse<KpTaskManageNewStatusDetailsVo> getById(@RequestParam String krtId) {
        try {
            log.debug("考评任务详情，参数:{}", krtId);
            return RestResponse.succeed(kpRatingTaskService.getById(krtId));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 计算面积占比
     *
     * @return
     */
    @GetMapping("areaProportionOperation")
    public RestResponse<List<KpAreaRatioOperationVo>> areaProportionOperation(@RequestParam String krtId) {
        try {
            return RestResponse.succeed(kpRatingTaskService.areaProportionOperation(krtId));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 面积占比页面
     *
     * @return
     */
    @GetMapping("areaProportionOperationPg")
    public RestResponse<List<KpAreaRatioOperationVo>> areaProportionOperationPg(@RequestParam String krtId) {
        try {
            return RestResponse.succeed(kpRatingTaskService.areaProportionOperationPg(krtId));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }


    /**
     * 查询省份面积占比
     *
     * @param provinceId 省id
     * @return
     */
    @GetMapping("getAreaRadioByProvinceId")
    public RestResponse<List<KpAreaRatioOperationVo>> getAreaRadioByProvinceId(@RequestParam String provinceId, @RequestParam String krtId) {
        try {
            log.debug("查询关联任务面积占比, param:{}", provinceId);
            return RestResponse.succeed(kpRatingTaskService.getAreaRadioByProvinceId(provinceId, krtId));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 查询考评任务总的面积占比
     *
     * @param kryId 考评任务id
     * @return
     */
    @GetMapping("getAreaRadio")
    public RestResponse<List<KpAreaRatioOperationVo>> getAreaRadio(@RequestParam String kryId) {
        try {
            log.debug("查询关联任务面积占比, param:{}", kryId);
            return RestResponse.succeed(kpRatingTaskService.getAreaRadio(kryId));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 计算系统评分
     *
     * @return
     */
    @GetMapping("systemScoreOps")
    public RestResponse<String> systemScoreOps(@RequestParam String krtId) {
        try {
            kpRatingTaskService.systemScoreOps(krtId);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 查询系统评分流程
     *
     * @param krtId
     * @return
     */
    @GetMapping("getSystemScoreOpsing")
    public RestResponse<List<String>> getSystemScoreOpsing(@RequestParam String krtId) {
        try {
            return RestResponse.succeed(kpRatingTaskService.getSystemScoreOpsing(krtId));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 计算省份系统评分
     *
     * @param krtId      考评任务id
     * @param provinceId 省份id
     * @return
     */
    @GetMapping("areaSystemScoreOps")
    public RestResponse<String> areaSystemScoreOps(@RequestParam String krtId, @RequestParam String provinceId) {
        try {
            kpRatingTaskService.systemScoreByProvinceIdTaskOps(krtId, provinceId);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 查询省份系统评分流程
     *
     * @param krtId      考评任务id
     * @param provinceId 省份id
     */
    @GetMapping("getAreaSystemScoreOpsing")
    public RestResponse<List<String>> getAreaSystemScoreOpsing(@RequestParam String krtId, @RequestParam String provinceId) {
        try {
            return RestResponse.succeed(kpRatingTaskService.getAreaSystemScoreOpsing(krtId, provinceId));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 删除考评任务
     *
     * @param krtId
     * @return
     */
    @GetMapping("deleteKpTask")
    public RestResponse<String> deleteKpTask(@RequestParam String krtId) {
        try {
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            if (currUid == null) {
                return RestResponse.fail("请先登录");
            }
            log.debug("删除考评任务,用户id:{},参数:{}", currUid, krtId);
            kpRatingTaskService.deleteKpTask(krtId, currUid);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 修改考评任务
     *
     * @return
     */
    @PostMapping("updateKpTask")
    public RestResponse<String> updateKpTask(@RequestBody UpdateKpTaskDto dto) {
        try {
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            if (currUid == null) {
                return RestResponse.fail("请先登录");
            }
            log.debug("修改考评任务,uid:{},参数:{}", currUid, JSONObject.toJSONString(dto));
            kpRatingTaskService.updateKpTask(currUid, dto);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 查询关联任务得分详情接口
     *
     * @param provinceId 省份id
     * @return
     */
    @GetMapping("getGlTaskScoreDetails")
    public RestResponse<List<KpIndexScoreDetailsVo>> getGlTaskScoreDetails(@RequestParam String provinceId, @RequestParam String krtId) {
        try {
            log.debug("查询关联任务得分详情,省id:{},考评任务id:{}", provinceId, krtId);
            return RestResponse.succeed(kpRatingTaskService.getGlTaskScoreDetails(provinceId, krtId));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }
    /**
     * 查询关联任务得分详情接口
     *
     * @param provinceId 省份id
     * @return
     */
    @GetMapping("getGlTaskScoreDetailsFinal")
    public RestResponse<List<KpIndexScoreDetailsVo>> getGlTaskScoreDetailsFinal(@RequestParam String provinceId, @RequestParam String krtId) {
        try {
            log.debug("查询关联任务得分详情,省id:{},考评任务id:{}", provinceId, krtId);
            return RestResponse.succeed(kpRatingTaskService.getGlTaskScoreDetailsFinal(provinceId, krtId));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 查询专家评分详情
     *
     * @param kesdi3Id 三级指标关系id
     * @return
     */
    @GetMapping("getExpertScoreDetails")
    public RestResponse<List<KpRatingTaskIndex3ScoreExpertVo>> getExpertScoreDetails(@RequestParam String kesdi3Id, @RequestParam String krtytId) {
        try {
            log.debug("查询专家评分详情,param:{}", kesdi3Id);
            return RestResponse.succeed(kpRatingTaskService.getExpertScoreDetails(kesdi3Id, krtytId));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 分发考评任务给专家
     *
     * @param krtId 考评任务id
     * @return
     */
    @GetMapping("distributionKpTask2Expert")
    public RestResponse<String> distributionKpTask2Expert(@RequestParam String krtId) {
        try {
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            if (currUid == null) {
                return RestResponse.fail("请先登录");
            }
            log.debug("分发考评任务给专家, param:{}", krtId);
            kpRatingTaskService.distributionKpTask2Expert(krtId, currUid);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 查询任务的专家
     *
     * @param krtId 考评任务id
     * @return
     */
    @GetMapping("getExpertByKrtId")
    public RestResponse<String> getExpertByGroupId(@RequestParam String krtId) {
        try {
            log.debug("getExpertByGroupId, param:{}", krtId);
            return RestResponse.succeed(kpRatingTaskService.getExpertByGroupId(krtId));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 停止评分
     *
     * @param krtId 考评任务id
     * @return
     */
    @GetMapping("stopTask")
    public RestResponse<String> stopTask(@RequestParam String krtId) {
        try {
            log.debug("停止评分,param:{}", krtId);
            kpRatingTaskService.stopTask(krtId);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 查看排名预览
     *
     * @param krtId 考评任务id
     * @return
     */
    @GetMapping("previewRanking")
    public RestResponse<KpTaskPreviewRankingVo> previewRanking(@RequestParam String krtId, @RequestParam(required = false, defaultValue = "score") String sortField) {
        log.debug("排名预览,param:{}, {}", krtId, sortField);
        KpTaskPreviewRankingVo kpTaskPreviewRankingVo = kpRatingTaskService.previewRanking(krtId, sortField);
        return RestResponse.succeed(kpTaskPreviewRankingVo);
    }

    /**
     * 考评排名查看查询考评任务列表
     *
     * @return
     */
    @GetMapping("rank/taskList")
    public RestResponse<List<KpRatingTask>> getTaskRankList(KpRatingTask kpRatingTask) {
        try {
            log.debug("查询考评任务列表，参数:{}", JSONObject.toJSONString(kpRatingTask));
            return RestResponse.succeed(kpRatingTaskService.getTaskRankList(kpRatingTask));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 修改排名
     *
     * @return
     */
    @PostMapping("updateRanking")
    public RestResponse<String> updateRanking(@RequestBody KpRatingTaskUpdateRankingDto kpRatingTaskUpdateRankingDto) {
        log.debug("修改排名,param:{}", JSONObject.toJSONString(kpRatingTaskUpdateRankingDto));
        kpRatingTaskService.updateRanking(kpRatingTaskUpdateRankingDto);
        return RestResponse.succeed();
    }

    /**
     * 发布排名
     *
     * @param krtId 考评任务id
     * @return
     */
    @GetMapping("publishRanking")
    public RestResponse<String> publishRanking(@RequestParam String krtId) {
        try {
            log.debug("发布排名,param:{}", krtId);
            kpRatingTaskService.publishRanking(krtId);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    @GetMapping("deleteCache")
    public RestResponse<String> deleteCache(@RequestParam String key) {
        try {
            log.debug("deleteCache,param:{}", key);
            kpRatingTaskService.deleteCache(key);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    //专家评分进度接口
    @GetMapping("expertScoreProgress")
    public RestResponse expertScoreProgress(@RequestParam String krtytId) {
        try {
            List<ExpertScoreProgressVO> list= kpRatingTaskService.expertScoreProgress(krtytId);
            return RestResponse.succeed(list);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }
}
